package web.pre;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import entity.News;
import entity.Product;
import entity.ProductCategory;
import service.NewsService;
import service.ProductCategoryService;
import service.ProductService;
import utils.PageUtils;

public class ProductServlet extends HttpServlet{

	public ProductService productService =new ProductService();

	public ProductCategoryService prcat=new ProductCategoryService();

	public ProductService  pro=new ProductService();

	public NewsService news=new NewsService();

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=UTF-8");
		//一个Servlet可以处理多个机试
		String action = request.getParameter("action");
		if(action!=null){
			switch (action) {
			case "queryList":
				queryList(request,response);
				break;
			case "queryDetails":
				queryDetaqails(request,response);
				break;
			default:
				break;
			}
		}else {
			response.sendRedirect("Regist.jsp");
		}
	}
	private void queryDetaqails(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException  {
		// TODO Auto-generated method stub
		int id=Integer.parseInt(request.getParameter("id"));
		Product produce = productService.findById(id);
		request.setAttribute("produce", produce);

		List<ProductCategory> listProductCategory =prcat.prcatList();

		Map<Integer,List<Product>> mapProductList=new HashMap<Integer, List<Product>>();

		for(ProductCategory productCategory : listProductCategory){
			if(productCategory.getType() == 1){
				List<Product>	 listProduct=pro.proList(productCategory.getId(),1,1,6);
				mapProductList.put(productCategory.getId(), listProduct);
			}
		}

		List<News> NewsList = news.NewsList();

		request.setAttribute("NewsList", NewsList);

		request.setAttribute("mapProductList", mapProductList);

		request.setAttribute("listProductCategory", listProductCategory);

		request.getRequestDispatcher("Product.jsp").forward(request, response);
	}
	private void queryList(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String categoryId= request.getParameter("categoryId");

		String categoryLeve=request.getParameter("categoryLeve1");

		int currPage=Integer.parseInt(request.getParameter("currPage"));//第一页
		int pageSize=4;//页面大小

		List<Product> productList =productService.proList(Integer.parseInt(categoryId),Integer.parseInt(categoryLeve),currPage,pageSize);
		//总记录数
		int totalCount = productService.procount(Integer.parseInt(categoryId),Integer.parseInt(categoryLeve));
		//工具类
		PageUtils<Product> pageUtil=new PageUtils<>();
		pageUtil.setCurrPage(currPage);
		pageUtil.setTotalCount(totalCount);
		pageUtil.setPageSize(pageSize);
		pageUtil.setList(productList);


		request.setAttribute("pageUtil", pageUtil);

		List<ProductCategory> listProductCategory =prcat.prcatList();

		Map<Integer,List<Product>> mapProductList=new HashMap<Integer, List<Product>>();

		for(ProductCategory productCategory : listProductCategory){
			if(productCategory.getType() == 1){
				List<Product>	 listProduct=pro.proList(productCategory.getId(),1,1,6);
				mapProductList.put(productCategory.getId(), listProduct);
			}
		}

		List<News> NewsList = news.NewsList();

		request.setAttribute("NewsList", NewsList);

		request.setAttribute("mapProductList", mapProductList);

		request.setAttribute("listProductCategory", listProductCategory);

		request.getRequestDispatcher("CategoryList.jsp").forward(request, response);
	}
	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		this.doGet(request, response);
	}
}

